package com.ygj.employee.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.persistent.model.Employee;
import com.cjy.common.bean.Files;
import com.cjy.common.bean.OpAttributes;
import com.ygj.inspection.service.EmployeeService;

@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class EmployeeSubHeadPicAction extends AbstractBaseAction{

	private static final long serialVersionUID = 1L;
	
	@Resource
	private EmployeeService employeeService;

	private int employeeId;
	
	/* Struts2文件上传的三个属性 */
	private File[] filePhotos;
	private String[] filePhotosFileName;
	private String[] filePhotosContentType;
	
	public File[] getFilePhotos() {
		return filePhotos;
	}
	public void setFilePhotos(File[] filePhotos) {
		this.filePhotos = filePhotos;
	}
	public String[] getFilePhotosFileName() {
		return filePhotosFileName;
	}
	public void setFilePhotosFileName(String[] filePhotosFileName) {
		this.filePhotosFileName = filePhotosFileName;
	}
	public String[] getFilePhotosContentType() {
		return filePhotosContentType;
	}
	public void setFilePhotosContentType(String[] filePhotosContentType) {
		this.filePhotosContentType = filePhotosContentType;
	}

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	/**
	 * 上传头像
	 * 
	 * @return
	 */
	@Action(value = "subHeadPic", interceptorRefs = { @InterceptorRef("fileUploadInterceptor") })
	public String subHeadPic() {
		try {
			Files files = new Files();
			files.setFile(filePhotos);
			files.setFileContentType(filePhotosContentType);
			files.setFileFileName(filePhotosFileName);
			// 因为是上传文件，用了common-upload的拦截器处理之后request会报空，要得到一般请求的request头，就要对request做个转型，才能调用相应方法
			HttpServletRequest servletRequest = new StrutsRequestWrapper(
					request);
			String basePath = null;
			String path = servletRequest.getContextPath();
			basePath = servletRequest.getScheme() + "://"
					+ ConfigConstants.SERVERIP + ":"
					+ servletRequest.getServerPort() + path + "/";
			int result = employeeService.subHeadPic(((List<Employee>)(request.getSession().getAttribute("users"))).get(0).getId(), 
					files, basePath);
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return  returnType();
	}

	/**
	 * 上传头像
	 *
	 * @return
	 */
	@Action(value = "subPingfenHeadPic", interceptorRefs = { @InterceptorRef("fileUploadInterceptor") })
	public String subPingfenHeadPic() {
		try {
			Files files = new Files();
			files.setFile(filePhotos);
			files.setFileContentType(filePhotosContentType);
			files.setFileFileName(filePhotosFileName);
			// 因为是上传文件，用了common-upload的拦截器处理之后request会报空，要得到一般请求的request头，就要对request做个转型，才能调用相应方法
			HttpServletRequest servletRequest = new StrutsRequestWrapper(
					request);
			String basePath = null;
			String path = servletRequest.getContextPath();
			basePath = servletRequest.getScheme() + "://"
					+ ConfigConstants.SERVERIP + ":"
					+ servletRequest.getServerPort() + path + "/";
			int result = employeeService.subHeadPic(employeeId,
					files, basePath);
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return  returnType();
	}

}
